﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using AmaroK86.MassEffect3.PCC;
using System.Text.RegularExpressions;

namespace AmaroK86.MassEffect3
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> pccList = new List<string>();
            //PCCFile pccFile = new PCCFile(@"E:\Mass Effect 3\BIOGame\CookedPCConsole\BioH_EDI_00.pcc");

            /*foreach (PCCFile.Import import in pccFile.importList)
            {
                Console.WriteLine(import.objectName + " is a " + import.className + " (" + import.packageFullName + ") from " + import.packageFile);
            }*/

            /*foreach (PCCFile.ExportEntry export in pccFile.Exports)
            {
                pccList.Add(export.PackageFullName + "." + export.ObjectName);// + "_" + BitConverter.ToInt32(export.data, 0));
                //Console.WriteLine(export.packageFullName + "." + export.objectName + ": " + BitConverter.ToInt32(export.data,0));// + " (" + export.packageFullName + ")");
            }
            pccList.Sort();

            foreach (string kvp in pccList)
            {
                Console.WriteLine(kvp);
            }*/

            /*Console.WriteLine("Filename: {0}", pccFile.fileName);
            Console.WriteLine("names count: {0}", pccFile.namesCount);
            Console.WriteLine("import count: {0}", pccFile.importsCount);
            Console.WriteLine("export count: {0}", pccFile.exportsCount);

            Console.WriteLine("export data begin: {0}", pccFile.exportList.Min(export => export.dataOffset));
            Console.WriteLine("export data end: {0}", pccFile.exportList.Max(export => export.dataOffset));

            for (int i = 0; i < pccFile.exportsCount; i++)
            {
                Console.WriteLine("0x" + pccFile.exportList[i].offset.ToString("X8") + ": " + pccFile.exportList[i].objectName + " from " + pccFile.exportList[i].className + " " + "." + pccFile.exportList[i].classParent + " class inside " + pccFile.exportList[i].packageFullName + "(" + pccFile.exportList[i].idxPackageName + ") package");
            }

            int maxIdx = pccFile.exportList.Max(export => BitConverter.ToInt32(export.data, 0));
            int minIdx = pccFile.exportList.Min(export => BitConverter.ToInt32(export.data, 0));
            /*Console.WriteLine("total exports: {0}, max idx: {1}, min idx: {2}", pccFile.exportsCount, maxIdx, minIdx);

            for (int i = 0; i < pccFile.importsCount; i++)
            {
                if (pccFile.importList[i].className == "Class")
                    Console.WriteLine("class {0} inside package {1} ({2})", pccFile.importList[i].objectName, pccFile.importList[i].packageName, pccFile.importList[i].packageFile);
                else
                    Console.WriteLine("object {0} from a {1} inside package {2} ({3})", pccFile.importList[i].objectName, (pccFile.importList[i].className == "Class") ? "Class" : pccFile.importList[i].className + " class", pccFile.importList[i].packageName, pccFile.importList[i].packageFile);
                //Console.WriteLine("0x" + pccFile.exportList[i].offset.ToString("X8") + ": " + pccFile.exportList[i].strName + " data size: " + pccFile.exportList[i].dataSize);
            }*/


            //pccFile.saveToFile();

            List<string> fileList = new List<string>();//Directory.GetFiles(ME3Paths.cookedPath, "*.pcc", SearchOption.AllDirectories).ToList();
            //fileList = fileList.Where(pccFile => Regex.IsMatch(pccFile,@".*INT\.pcc|.*ITA\.pcc")).ToList();
            fileList.Add("BioD_KroGru_700Queen.pcc");
            //fileList.Add("EntryMenu.pcc");
            int pccTotalCount = fileList.Count;
            int pccCount = 0;
            int pccWithTexCount = 0;
            PCCObject pccFile;

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();

            foreach (string inputPath in fileList)
            {
                pccFile = new PCCObject(inputPath);
                int pccTexCount = 0;
                foreach (PCCObject.ExportEntry export in pccFile.Exports)
                {
                    if (export.ClassName == "Texture2D")
                    {
                        //Console.WriteLine("    Found " + export.ObjectName);
                        pccTexCount++;
                        break;
                    }
                }
                if (pccTexCount != 0)
                {
                    pccWithTexCount++;
                }
                else
                    Console.WriteLine(pccCount + "/" + pccTotalCount + " - " + Path.GetFileName(inputPath));

                pccCount++;
                //Console.WriteLine(pccCount + "/" + pccTotalCount + " " + Path.GetFileName(inputPath) + ": " + pccTexCount + " textures");
            }

            stopWatch.Stop();
            // Get the elapsed time as a TimeSpan value.
            TimeSpan ts = stopWatch.Elapsed;

            Console.WriteLine("Pcc with textures: {0}", pccWithTexCount);

            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                ts.Hours, ts.Minutes, ts.Seconds,
                ts.Milliseconds / 10);
            Console.WriteLine("RunTime: {0}", elapsedTime);


            pccFile = new PCCObject(fileList[0]);
            pccFile.saveToFile(true);

            Console.WriteLine("Done.");

            Console.ReadLine();
        }

        public static void Write(PCCObject.ExportEntry export)
        {
            Console.WriteLine("Object: " + export.ObjectName + " with package: " + export.PackageName + "(" + export.idxPackageName + ")");
        }
    }
}
